<template>
  <div class="permissions">
    <el-card style="margin: 20px">
      <el-row :column="3" :gutter="20">
        <el-form :inline="true" ref="form" :model="form">
          <el-col :span="4">
            <el-form-item>
              <el-input
                class="search"
                v-model="form.searchValue"
                placeholder="根据用户名查找"
                size="small"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4" style="margin-left:20px">
            <el-button size="small" @click="onClear">{{$t("route.reset")}}</el-button>
            <el-button type="primary" size="small" @click="onSearch">{{$t("table.search")}}</el-button>
          </el-col>
          <el-col :span="16">
            <el-form-item class="add">
              <el-form-item>
                <el-button icon="el-icon-edit" type="success" size="small" @click="onAdd"
                  >{{$t("table.addPermissions")}}</el-button
                >
              </el-form-item>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <el-alert
        style="width: 100%; margin-bottom: 15px"
        :title="`共${total}条记录`"
        type="info"
        show-icon
        :closable="false"
      ></el-alert>
      <!-- 表格区 -->
      <el-table
        v-loading="loading"
        element-loading-text="给我一点时间"
        :data="tableData"
        highlight-current-row
        :header-cell-style="{ background: '#FAFAFA' }"
      >
        <el-table-column type="selection" > </el-table-column>
        <el-table-column align="center" prop="title" :label="$t('table.username')">
        </el-table-column>
        <el-table-column sortable prop="create_date" :label="$t('table.date')" >
          <template slot-scope="{row}" >
            {{row.create_date | format}}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="" :label="$t('table.actions')">
          <template slot-scope="{ row }">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              plain
              @click="onEdit(row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              plain
              @click="onDelete(row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 页码区 -->
      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="query.page"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="query.pagesize"
        :total="total"
        layout="sizes, prev, pager, next, jumper"
      >
      </el-pagination>
      <!-- 弹层 -->
      <permissions-add
        v-model="permissionAddDialogIsvisible"
        :title="title"
        :rowData="rowData"
        @on-success='loaingPermissionList'
      />
    </el-card>
  </div>
</template>

<script>
import { list, remove } from '@/api/base/permissions'
import permissionsAdd from '../components/permissions-add.vue'
export default {
  components: { permissionsAdd },
  data () {
    return {
      form: {
        searchValue: ''
      },
      query: {
        page: 1,
        pagesize: 10,
        title: '',
        create_date: ''
      },
      delText: {
        id: ''
      },
      tableData: [],
      total: 0,
      title: {
        text: '',
        pageTitle: ''
      },
      loading: true,
      rowData: {},
      permissionAddDialogIsvisible: false
    }
  },

  created () {
    this.loaingPermissionList()
  },

  methods: {
    async loaingPermissionList () {
      const res = await list(this.query)
      this.total = res.data.counts
      this.tableData = res.data.list
      this.loading = false
      // console.log(res)
    },
    handleSizeChange (val) {
      this.query.pagesize = val
      this.loaingPermissionList()
    },
    handleCurrentChange (val) {
      this.query.page = val
      this.loaingPermissionList()
    },
    onClear () {
      this.form.searchValue = ''
      this.query.title = ''
      this.loaingPermissionList()
    },
    async onSearch () {
      this.query.title = this.form.searchValue
      const { data } = await list(this.query)
      this.tableData = data.list
      this.total = data.counts
    },
    onAdd () {
      this.title.text = '创建'
      this.title.pageTitle = '权限组'
      // this.formBase = {}
      // this.$set(this.formBase, 'permissions', [])
      this.permissionAddDialogIsvisible = true
    },
    onEdit (row) {
      this.rowData = { ...row }
      this.$set(this.rowData, 'permissions', [])
      this.title.text = '编辑'
      this.title.pageTitle = '权限组'
      this.permissionAddDialogIsvisible = true
      // console.log(this.rowData)
    },
    onDelete (row) {
      this.delText.id = row.id
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          if (this.tableData.length === 1 && this.query.page !== 1) {
            this.query.page--
          }
          await remove(this.delText)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.loaingPermissionList()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style scoped lang="less">
/deep/.search {
  width: 200px;
}
.el-form {
  display: flex;
}
/deep/.add {
  float: right;
}
/deep/.pagination {
  float: right;
  padding: 20px 0;
}
/deep/.el-table{
  width: 100%;
  margin: 15px 0;
}
</style>
